import logging

import aiohttp


class AsyncRequest:
    """异步的请求"""
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/83.0.4103.61 Safari/537.36 "
    }

    @classmethod
    async def aget(cls, url):
        """异步发起GET请求 默认返回text"""
        async with aiohttp.ClientSession() as session:
            async with await session.get(url, headers=cls.headers) as response:
                logging.info("请求列表url:%s,响应码是：%d" % (url, response.status))
                print("请求url:%s,响应码是：%d" % (url, response.status))
                if response.status == 200:
                    return await response.text()
                return None

    @classmethod
    async def getByte(cls, url):
        async with aiohttp.ClientSession() as session:
            async with await session.get(url, headers=cls.headers) as response:
                # logging.info("请求url:%s,响应码是：%d" % (url, response.status))
                # print("请求url:%s,响应码是：%d" % (url, response.status))
                if response.status == 200:
                    return await response.read()
                return None
